<template>
	<div>
		<CommonSearch v-model="tableConfig.searchParams" :config="searchConfig"></CommonSearch>
		<CommonTable :config="tableConfig" ref="commonTable"></CommonTable>
	</div>
</template>

<script lang="javascript">
	import { ref, reactive, toRefs, onMounted, getCurrentInstance } from 'vue';
	import { CREATE, UPDATE, DELETE, QUERY, IMPORT } from '@/utils/Const';
	import { useFactoryRouter } from '@/hooks/useFactoryRouter';
	import {
		getDictListApi,
		getDictDataListApi,
		createDictDataApi,
		updateDictDataApi,
		deleteDictDataApi
	} from '@/api/system.api';
	export default {
		name : 'DictionaryData',
		setup () {
			let { route } = useFactoryRouter();
			let Instance = getCurrentInstance().proxy;
			let dictList = ref([]);
			let commonTable = null;
			let data = reactive({
				searchConfig : {
					handlerSearch () {
						return commonTable.fetchData({ isResetPageNumber : true });
					},
					form : [
						{
							type : 'select',
							label : '字典名称:',
							placeholder : '请输入字典名称',
							key : 'dictType',
							defaultValue : Number(route.params.id),
							options : []
						},
						{
							type : 'text',
							label : '字典标签:',
							placeholder : '请输入字典标签',
							key : 'dictLabel'
						},
						{
							type : 'select',
							label : '状态:',
							placeholder : '请选择状态',
							key : 'status',
							options : [
								{
									value : '',
									label : '全部'
								},
								{
									value : '1',
									label : '正常'
								},
								{
									value : '0',
									label : '停用'
								}
							]
						}
					]
				},
				tableConfig : {
					searchParams : {},
					reqApiMethod : getDictDataListApi,
					handlerOperate : (payload, row) => {
						data.formConfig.type = payload.butType;
						let formConfig = {
							data : Array.isArray(row) ? row[0] : '',
							...data.formConfig
						};
						let names = [];
						let ids = [];
						switch (payload.butType) {
						case CREATE:
							formConfig.form.forEach((item) => {
								if (item.key === 'dictType') {
									item.defaultValue = data.tableConfig.searchParams.dictType;
								}
							});
							Instance.$hyDialogForm(data.formConfig);
							break;
						case DELETE:
							row.forEach((item) => {
								names.push(item.dictLabel);
								ids.push(item.dictCode);
							});
							Instance.$hyConfirm({
								subTitle : `是否确定删除【${names.join()}】`,
								callback : async (done) => {
									await deleteDictDataApi({ ids : ids.join() });
									done();
									commonTable.fetchData();
								}
							});
							break;
						case UPDATE:
							Instance.$hyDialogForm(formConfig);
							break;
						case QUERY:
							formConfig.disabled = true;
							Instance.$hyDialogForm(formConfig);
							break;
						}
					},
					mergeTable : () => {},
					columns : [
						{
							type : 'index'
						},
						{
							type : 'selection'
						},
						{
							title : '字典编码',
							field : 'dictCode'
						},
						{
							title : '字典标签',
							field : 'dictLabel'
						},
						{
							title : '字典键值',
							field : 'dictValue'
						},
						{
							title : '字典排序',
							field : 'dictSort'
						},
						{
							title : '状态',
							field : 'status',
							formatter : (value) => {
								if (Number(value) === 0) {
									return '停用';
								}
								return '正常';
							}
						},
						{
							title : '备注',
							field : 'remark'
						},
						{
							title : '创建时间',
							field : 'createTime'
						}
					],
					globalOperate : [
						{
							butType : CREATE,
							name : '新增',
							color : 'primary',
							role : [],
							icon : 'el-icon-plus'
						},
						{
							butType : DELETE,
							name : '删除',
							enable : '2',
							color : 'danger',
							role : [],
							icon : 'el-icon-delete'
						},
						{
							butType : UPDATE,
							name : '修改',
							enable : '1',
							color : 'success',
							role : [],
							icon : 'el-icon-edit'
						},
						{
							butType : QUERY,
							name : '查看',
							enable : '1',
							color : 'warning',
							role : [],
							icon : 'el-icon-edit'
						},
						{
							butType : IMPORT,
							name : '导出',
							color : 'warning',
							role : [],
							icon : 'el-icon-download'
						}
					],
					operate : [
						{
							butType : UPDATE,
							type : 'text',
							name : '修改',
							role : [],
							icon : 'el-icon-edit'
						},
						{
							butType : DELETE,
							type : 'text',
							name : '删除',
							role : [],
							icon : 'el-icon-delete'
						}
					]
				},
				formConfig : {
					type : '',
					async handlerSubmit (params) {
						switch (data.formConfig.type) {
						case CREATE:
							await createDictDataApi(params);
							break;
						case UPDATE:
							await updateDictDataApi(params);
							break;
						}
						return commonTable.fetchData();
					},
					form : [
						{
							type : 'select',
							label : '字典类型:',
							placeholder : '请选择字典类型',
							maxlength : 10,
							disabled : true,
							key : 'dictType',
							rules : [
								{ required : true, message : '请选择字典类型', trigger : 'blur' }
							],
							options : dictList
						},
						{
							type : 'text',
							label : '数据标签:',
							placeholder : '请输入数据标签',
							maxlength : 10,
							key : 'dictLabel',
							rules : [
								{ required : true, message : '请输入数据标签', trigger : 'blur' }
							]
						},
						{
							type : 'text',
							label : '数据键值:',
							placeholder : '请输入数据键值',
							maxlength : 10,
							key : 'dictValue',
							rules : [
								{ required : true, message : '请输入数据键值', trigger : 'blur' }
							]
						},
						{
							type : 'radio',
							label : '状态:',
							key : 'status',
							defaultValue : '1',
							options : [
								{
									value : '1',
									label : '正常'
								},
								{
									value : '0',
									label : '停用'
								}
							]
						},
						{
							type : 'textarea',
							label : '备注:',
							placeholder : '请输入内容',
							key : 'remark',
							maxlength : 100
						}
					]
				}
			});

			let init = async () => {
				let res = await getDictListApi();
				if (res && res.list && res.list) {
					dictList.value = res.list.map((item) => {
						return { value : item.dictId, label : item.dictName };
					});
				}
				data.searchConfig.form.forEach((item) => {
					if (item.key === 'dictType') {
						item.options = dictList.value;
					}
				});
			};

			init();

			onMounted(() => {
				commonTable = Instance.$refs['commonTable'];
			});
			return {
				...toRefs(data)
			};
		}
	};
</script>

<style lang="scss" scoped></style>
